package com.customer.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.customer.mall.domain.Product;
import com.customer.mall.domain.vo.ProductVO;
import com.customer.mall.service.IProductService;
import com.customer.mall.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiGGG
 * @since 2023-06-25
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @GetMapping("/list")
    public ResponseResult<Page<ProductVO>> pageProduct(String name,
                                                       String categoryName,
                                                       @RequestParam(defaultValue = "1") Integer pageNum,
                                                       @RequestParam(defaultValue = "5") Integer pageSize){
        return productService.pageProduct(name, categoryName, pageNum, pageSize);
    }

    @GetMapping("/admin/{id}")
    public ResponseResult<ProductVO> getProductById(@PathVariable Integer id){
        return productService.getProductById(id);
    }

    @PostMapping("/admin/insert")
    public ResponseResult<String> insertProduct(@RequestBody Product product){
        return productService.insertProduct(product);
    }

    @PostMapping("/admin/update")
    public ResponseResult<String> updateProduct(@RequestBody Product product){
        return productService.updateProduct(product);
    }

    @PostMapping("/admin/delete")
    public ResponseResult<String> deleteProduct(@RequestBody List<Integer> ids){
        return productService.removeBatchByIds(ids) ? ResponseResult.okResult() : ResponseResult.failResult();
    }

}
